package tree;
import java.util.LinkedList;
import java.util.Queue;

public class Tree {
	private Node root;

	public Tree(int value) {
		root = new Node(value, 0);
	}

	public void insertValue(int value) {
		insert(root, value);
	}

	private void insert(Node node, int value) {
		if (node.getValue() < value)
			if (node.getRight() == null)
				node.setRight(new Node(value, node.getDepth() + 1));
			else
				insert(node.getRight(), value);
		else if (node.getLeft() == null)
			node.setLeft(new Node(value, node.getDepth() + 1));
		else
			insert(node.getLeft(), value);
	}

	public void removeValue(int value) {

	}

	public void printTree() {
		String separator = "\n=============================";
		for (DepthFirstTraversal type : DepthFirstTraversal.values()) {
			System.out.println(separator + "\nDepth first " + type + separator);
			depthFirst(root, type);
		}
		System.out.println(separator + "\nBreadth first" + separator);
		bredthFirst();
	}

	private void bredthFirst() {
		Queue<Node> queue = new LinkedList<Node>();
		queue.add(root);
		while (!queue.isEmpty()) {
			Node current = queue.remove();
			System.out.println("value: " + current);
			if (current.getLeft() != null)
				queue.add(current.getLeft());
			if (current.getRight() != null)
				queue.add(current.getRight());
		}
	}

	private void depthFirst(Node node, DepthFirstTraversal type) {
		if (type == DepthFirstTraversal.PRE_ORDER)
			System.out.println(node); // PRE-ORDER

		if (node.getLeft() != null)
			depthFirst(node.getLeft(), type);

		if (type == DepthFirstTraversal.IN_ORDER)
			System.out.println(node); // IN-ORDER

		if (node.getRight() != null)
			depthFirst(node.getRight(), type);

		if (type == DepthFirstTraversal.POST_ORDER)
			System.out.println(node); // POST-ORDER
	}

	public Node getRoot() {
		return root;
	}

	public void setRoot(Node root) {
		this.root = root;
	}
}
